home *** CD-ROM | disk | FTP | other *** search
- // itgdmvw.cpp : implementation of the CItgDemoView class
- //
-
- #include "stdafx.h"
- #include "itgdemo.h"
-
- #include "mainfrm.h"
- #include "itgdmdoc.h"
- #include "itgdmvw.h"
- #include "dlgitem.h"
- #include "dlgline.h"
-
- #include "itgdefs.h"
-
- #ifdef _DEBUG
- #undef THIS_FILE
- static char BASED_CODE THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CItgDemoView
-
- IMPLEMENT_DYNCREATE(CItgDemoView, CFormView)
-
- BEGIN_MESSAGE_MAP(CItgDemoView, CFormView)
- //{{AFX_MSG_MAP(CItgDemoView)
- ON_WM_SIZE()
- ON_VBXEVENT(VBN_ITEMCONNECT, IDC_ITGRAPH1, OnItemconnectItgraph1)
- ON_VBXEVENT(VBN_KEYPRESS, IDC_ITGRAPH1, OnKeypressItgraph1)
- ON_VBXEVENT(VBN_ITEMCLICK, IDC_ITGRAPH1, OnItemclickItgraph1)
- ON_VBXEVENT(VBN_CLICK, IDC_ITGRAPH1, OnClickItgraph1)
- ON_VBXEVENT(VBN_SELECTRECT, IDC_ITGRAPH1, OnSelectrectItgraph1)
- ON_VBXEVENT(VBN_ITEMDBLCLICK, IDC_ITGRAPH1, OnItemdblclickItgraph1)
- ON_VBXEVENT(VBN_DBLCLICK, IDC_ITGRAPH1, OnDblclickItgraph1)
- ON_VBXEVENT(VBN_ITEMMOUSEMOVE, IDC_ITGRAPH1, OnItemmousemoveItgraph1)
- ON_VBXEVENT(VBN_MOUSEDOWN, IDC_ITGRAPH1, OnMousedownItgraph1)
- ON_VBXEVENT(VBN_LINEDBLCLICK, IDC_ITGRAPH1, OnLinedblclickItgraph1)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CItgDemoView construction/destruction
-
- CItgDemoView::CItgDemoView()
- : CFormView(CItgDemoView::IDD)
- {
- //{{AFX_DATA_INIT(CItgDemoView)
- m_ITGraph = NULL;
- //}}AFX_DATA_INIT
-
- m_sFindStr = "";
- m_bRandomSizedNodes = FALSE;
- m_nPrintoutType = 1; // Print the Graph
- }
-
- CItgDemoView::~CItgDemoView()
- {
- }
-
- void CItgDemoView::DoDataExchange(CDataExchange* pDX)
- {
- CFormView::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CItgDemoView)
- DDX_VBControl(pDX, IDC_ITGRAPH1, m_ITGraph);
- //}}AFX_DATA_MAP
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CItgDemoView printing
-
- BOOL CItgDemoView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- if(m_nPrintoutType == 1) {
- HDC hGraphDC = ::GetDC(m_ITGraph->m_hWnd);
- ptPix_G.x = ::GetDeviceCaps(hGraphDC, LOGPIXELSX);
- ptPix_G.y = ::GetDeviceCaps(hGraphDC, LOGPIXELSY);
- ::DeleteDC(hGraphDC);
-
- m_ITGraph->SetNumProperty("PrintToDC", 0); // sets SelectRect to entire graph
- rcGraph.left = (int)m_ITGraph->GetNumProperty("SelectRectLeft");
- rcGraph.top = (int)m_ITGraph->GetNumProperty("SelectRectTop");
- rcGraph.right = rcGraph.left + (int)m_ITGraph->GetNumProperty("SelectRectWidth");
- rcGraph.bottom = rcGraph.top + (int)m_ITGraph->GetNumProperty("SelectRectHeight");
-
- int scale = (int)m_ITGraph->GetNumProperty("DrawScale");
-
- mInches_X = MulDiv(rcGraph.Width(), (scale ? scale : 100), ptPix_G.x);
- nPages_X = mInches_X / 750;
- if(mInches_X % 750)
- nPages_X++;
-
- mInches_Y = MulDiv(rcGraph.Height(), (scale ? scale : 100), ptPix_G.y);
- nPages_Y = mInches_Y / 900;
- if(mInches_Y % 900)
- nPages_Y++;
-
- rcPage_G.left = 0;
- rcPage_G.top = 0;
- rcPage_G.right = MulDiv(rcGraph.Width(), 750, mInches_X);
- rcPage_G.bottom = MulDiv(rcGraph.Height(), 900, mInches_Y);
-
- pInfo->SetMaxPage(nPages_X * nPages_Y);
- }
-
- return DoPreparePrinting(pInfo);
- }
-
- void CItgDemoView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
- {
- if(m_nPrintoutType == 1) { // printing the entire graph; we must set it up
- ptPix_P.x = pDC->GetDeviceCaps(LOGPIXELSX);
- ptPix_P.y = pDC->GetDeviceCaps(LOGPIXELSY);
-
- int scale = (int)m_ITGraph->GetNumProperty("DrawScale");
-
- rcPrintout.left = 0;
- rcPrintout.top = 0;
- rcPrintout.right = rcPrintout.left + MulDiv(mInches_X, ptPix_P.x, 100);
- rcPrintout.bottom = rcPrintout.top + MulDiv(mInches_Y, ptPix_P.y, 100);
-
- rcPage_P.left = ptPix_P.x / 2;
- rcPage_P.top = ptPix_P.y;
- rcPage_P.right = rcPage_P.left + MulDiv(750, ptPix_P.x, 100);
- rcPage_P.bottom = rcPage_P.top + MulDiv(900, ptPix_P.y, 100);
- }
- }
-
- void CItgDemoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
-
- void CItgDemoView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
- {
- if(m_nPrintoutType == 1) {
- CRect rcSel;
- rcSel.left = rcGraph.left + ((pInfo->m_nCurPage-1)%nPages_X) * rcPage_G.Width();
- rcSel.top = rcGraph.top + ((pInfo->m_nCurPage-1)/nPages_X) * rcPage_G.Height();
- rcSel.right = rcSel.left + rcPage_G.Width();
- rcSel.bottom = rcSel.top + rcPage_G.Height();
- m_ITGraph->SetNumProperty("SelectRectLeft", rcSel.left);
- m_ITGraph->SetNumProperty("SelectRectTop", rcSel.top);
- m_ITGraph->SetNumProperty("SelectRectWidth", rcSel.Width());
- m_ITGraph->SetNumProperty("SelectRectHeight", rcSel.Height());
-
- m_ITGraph->SetNumProperty("PrintRectLeft", rcPage_P.left);
- m_ITGraph->SetNumProperty("PrintRectTop", rcPage_P.top);
- m_ITGraph->SetNumProperty("PrintRectWidth", rcPage_P.Width());
- m_ITGraph->SetNumProperty("PrintRectHeight", rcPage_P.Height());
-
- char tmpStr[80];
- int charWidth = pDC->GetTextExtent("M", 1).cx;
- int charHeight = pDC->GetTextExtent("M", 1).cy;
- int oldTextAlign = pDC->SetTextAlign(TA_CENTER + TA_TOP);
- sprintf(tmpStr, "ITGraph Property Playground");
- pDC->TextOut(rcPage_P.left + rcPage_P.Width()/2, rcPage_P.top - 3*charHeight,
- tmpStr, strlen(tmpStr));
- pDC->SetTextAlign(TA_CENTER + TA_BOTTOM);
- sprintf(tmpStr, "Page %d of %d", pInfo->m_nCurPage, nPages_X * nPages_Y);
- pDC->TextOut(rcPage_P.left + rcPage_P.Width()/2, rcPage_P.top + rcPage_P.Height() + 3*charHeight,
- tmpStr, strlen(tmpStr));
- pDC->SetTextAlign(oldTextAlign);
-
- pDC->ExcludeClipRect(pInfo->m_rectDraw.left-ptPix_P.x, pInfo->m_rectDraw.top-ptPix_P.y,
- rcPage_P.left - charWidth, pInfo->m_rectDraw.bottom+ptPix_P.y);
- pDC->ExcludeClipRect(rcPage_P.right + charWidth, pInfo->m_rectDraw.top-ptPix_P.y,
- pInfo->m_rectDraw.right+ptPix_P.x, pInfo->m_rectDraw.bottom+ptPix_P.y);
- pDC->ExcludeClipRect(pInfo->m_rectDraw.left-ptPix_P.x, pInfo->m_rectDraw.top-ptPix_P.y,
- pInfo->m_rectDraw.right+ptPix_P.x, rcPage_P.top - charHeight);
- pDC->ExcludeClipRect(pInfo->m_rectDraw.left-ptPix_P.x, rcPage_P.bottom + charHeight,
- pInfo->m_rectDraw.right+ptPix_P.x, pInfo->m_rectDraw.bottom+ptPix_P.y);
- m_ITGraph->SetNumProperty("PrintToDC", (int)pDC->m_hDC);
- pDC->SelectClipRgn(NULL);
- } else {
- char tmpStr[80];
- int charWidth = pDC->GetTextExtent("M", 1).cx;
- int charHeight = pDC->GetTextExtent("M", 1).cy;
-
- CPoint pos(4 * charWidth, 2 * charHeight);
-
- pDC->TextOut(pos.x, pos.y, "ITGraph Property Playground");
- pos.y += charHeight;
- pDC->TextOut(pos.x, pos.y, "Current Graph and Associated Statistics");
- pos.y += 2 * charHeight; // skip a line
-
- // compute bounding rectangle for the graph, 1/3 of page width, 1/5 of page height
- CRect rcGraph(pos.x, pos.y, pos.x + pDC->GetDeviceCaps(HORZRES)/3,
- pos.y + pDC->GetDeviceCaps(VERTRES)/5);
- // draw rectangle around graph
- pDC->Rectangle(rcGraph);
-
- m_ITGraph->SetNumProperty("PrintToDC", 0); // select entire graph for printing
- // select the target print area
- m_ITGraph->SetNumProperty("PrintRectLeft", rcGraph.left);
- m_ITGraph->SetNumProperty("PrintRectTop", rcGraph.top);
- m_ITGraph->SetNumProperty("PrintRectWidth", rcGraph.Width());
- m_ITGraph->SetNumProperty("PrintRectHeight", rcGraph.Height());
- m_ITGraph->SetNumProperty("PrintToDC", (int)pDC->m_hDC); // print the graph
-
- pos.y += rcGraph.Height() + charHeight;
-
- // print the names of all nodes in the graph
- int nItems = (int)m_ITGraph->GetNumProperty("ListCount");
- sprintf(tmpStr, "The Graph Contains %d Nodes", nItems);
- pDC->TextOut(pos.x, pos.y, tmpStr, strlen(tmpStr));
- pos.y += charHeight;
- for(int i = 0; i < nItems; i++) {
- sprintf(tmpStr, " Node %d: %s", i, m_ITGraph->GetStrProperty("List", i));
- pDC->TextOut(pos.x, pos.y, tmpStr, strlen(tmpStr));
- pos.y += charHeight;
- }
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CItgDemoView diagnostics
-
- #ifdef _DEBUG
- void CItgDemoView::AssertValid() const
- {
- CFormView::AssertValid();
- }
-
- void CItgDemoView::Dump(CDumpContext& dc) const
- {
- CFormView::Dump(dc);
- }
-
- CItgDemoDoc* CItgDemoView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CItgDemoDoc)));
- return (CItgDemoDoc*)m_pDocument;
- }
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // CItgDemoView message handlers
-
- void CItgDemoView::OnSize(UINT nType, int cx, int cy)
- {
- CFormView::OnSize(nType, cx, cy);
-
- if(m_ITGraph) {
- CRect rcClient;
- GetClientRect(&rcClient);
- m_ITGraph->EnableVBXFloat();
- m_ITGraph->Move(rcClient);
- }
- }
-
- void CItgDemoView::OnItemconnectItgraph1(UINT, int, CWnd*, LPVOID lpParams)
- {
- short fromIx = AFX_NUM_EVENTPARAMINDEX(short, lpParams, 5);
- short toIx = AFX_NUM_EVENTPARAMINDEX(short, lpParams, 3);
-
- m_ITGraph->SetNumProperty("ConnectTo", toIx, fromIx);
- }
-
- void CItgDemoView::OnItemclickItgraph1(UINT, int, CWnd*, LPVOID lpParams)
- {
- short ItemIx = AFX_NUM_EVENTPARAMINDEX(short, lpParams, 5);
- short ItemHandle = AFX_NUM_EVENTPARAMINDEX(short, lpParams, 4);
- if(ItemHandle == 0) {
- m_ITGraph->SetNumProperty("SelectedIndex", ItemIx);
- }
- }
-
- void CItgDemoView::OnClickItgraph1(UINT, int, CWnd*, LPVOID lpParams)
- {
- short Button = AFX_NUM_EVENTPARAMINDEX(short, lpParams, 3);
- if(Button == ITG_LeftButton)
- m_ITGraph->SetNumProperty("SelectedIndex", -1);
- }
-
- void CItgDemoView::OnMousedownItgraph1(UINT, int, CWnd*, LPVOID lpParams)
- {
- short Button = AFX_NUM_EVENTPARAMINDEX(short, lpParams, 3);
- if(Button == ITG_RightButton) {
- CMenu popMenu;
-
- int isSelected = m_ITGraph->GetNumProperty("SelectedIndex") != -1;
- if(isSelected)
- popMenu.LoadMenu(IDR_POPUP2);
- else
- popMenu.LoadMenu(IDR_POPUP1);
-
- CMenu *pPopup = popMenu.GetSubMenu(0);
- POINT pt;
- ::GetCursorPos(&pt);
- pPopup->TrackPopupMenu(TPM_LEFTALIGN + TPM_RIGHTBUTTON, pt.x, pt.y, GetParent());
- }
- }
-
- void CItgDemoView::OnKeypressItgraph1(UINT, int, CWnd*, LPVOID lpParams)
- {
- if(m_ITGraph->GetNumProperty("SelectedIndex") >= 0) {
- short KeyCode = AFX_NUM_EVENTPARAMINDEX(short, lpParams, 0);
- CString sName = m_ITGraph->GetStrProperty("List", (int)m_ITGraph->GetNumProperty("SelectedIndex"));
-
- if((KeyCode == '\b') && (sName.GetLength() > 0)) {
- if((sName.GetLength() >= 2) && (sName.Right(2) == "\r\n"))
- sName = sName.Left(sName.GetLength()-2);
- else if(sName.GetLength() >= 1)
- sName = sName.Left(sName.GetLength()-1);
- } else if(KeyCode == '\r')
- sName += "\r\n";
- else if(KeyCode >= ' ')
- sName += CString((char)KeyCode);
-
- m_ITGraph->SetStrProperty("List", sName, (int)m_ITGraph->GetNumProperty("SelectedIndex"));
- }
- }
-
- void CItgDemoView::OnSelectrectItgraph1(UINT, int, CWnd*, LPVOID lpParams)
- {
- short L = AFX_NUM_EVENTPARAMINDEX(short, lpParams, 5);
- short T = AFX_NUM_EVENTPARAMINDEX(short, lpParams, 4);
- short W = AFX_NUM_EVENTPARAMINDEX(short, lpParams, 3);
- short H = AFX_NUM_EVENTPARAMINDEX(short, lpParams, 2);
-
- m_ITGraph->SetNumProperty("SelectRectLeft", L);
- m_ITGraph->SetNumProperty("SelectRectTop", T);
- m_ITGraph->SetNumProperty("SelectRectWidth", W);
- m_ITGraph->SetNumProperty("SelectRectHeight", H);
- m_ITGraph->SetNumProperty("ZoomSelectRect", TRUE);
- }
-
- void CItgDemoView::OnItemdblclickItgraph1(UINT, int, CWnd*, LPVOID lpParams)
- {
- short Button = AFX_NUM_EVENTPARAMINDEX(short, lpParams, 3);
- short ItemHandle = AFX_NUM_EVENTPARAMINDEX(short, lpParams, 4);
- short ItemIx = AFX_NUM_EVENTPARAMINDEX(short, lpParams, 5);
-
- if((Button == ITG_LeftButton) && (ItemHandle == 0)) {
- CDlgItem dlgItem(m_ITGraph, this);
- dlgItem.DoModal();
- }
- }
-
- void CItgDemoView::OnDblclickItgraph1(UINT, int, CWnd*, LPVOID)
- {
- m_ITGraph->SetNumProperty("DrawScale", 0);
- }
-
- void CItgDemoView::OnItemmousemoveItgraph1(UINT, int, CWnd*, LPVOID)
- {
- }
-
- void CItgDemoView::FindNode(CWnd *parWnd, CString sFindStr)
- {
- int nItems = (int)m_ITGraph->GetNumProperty("ListCount");
- int selIx = (int)m_ITGraph->GetNumProperty("SelectedIndex");
-
- for(int i = 1; i < nItems; i++) {
- int itemIx = (selIx + i) % nItems;
- CString sList = m_ITGraph->GetStrProperty("List", itemIx);
- if(sList.Find(sFindStr) != -1) {
- m_ITGraph->SetNumProperty("SelectRectLeft", m_ITGraph->GetNumProperty("ItemXpos", itemIx));
- m_ITGraph->SetNumProperty("SelectRectTop", m_ITGraph->GetNumProperty("ItemYpos", itemIx));
- m_ITGraph->SetNumProperty("SelectRectWidth", m_ITGraph->GetNumProperty("ItemWidth", itemIx));
- m_ITGraph->SetNumProperty("SelectRectHeight", m_ITGraph->GetNumProperty("ItemHeight", itemIx));
- m_ITGraph->SetNumProperty("ZoomSelectRect", FALSE); // scroll w/o scale change
- m_ITGraph->SetNumProperty("SelectedIndex", itemIx);
- return;
- }
- }
-
- parWnd->MessageBox("The string '" + sFindStr + "' was not found.");
- }
-
- void CItgDemoView::RandomTree(int NodeCount)
- {
- if(AskToSave()) {
- int oldRedraw = (int)m_ITGraph->GetNumProperty("Redraw");
- m_ITGraph->SetNumProperty("Redraw", FALSE);
- m_ITGraph->SendMessage(VBM_METHOD, METH_CLEAR, 0L);
- for(int i = 1; i <= NodeCount; i++) {
- char szNodeName[16];
- sprintf(szNodeName, "Node #%d", i);
- m_ITGraph->AddItem(szNodeName, -1);
- if(m_bRandomSizedNodes) {
- m_ITGraph->SetNumProperty("ItemWidth", rand()%200 + 25, i-1);
- m_ITGraph->SetNumProperty("ItemHeight", rand()%200 + 25, i-1);
- }
- if(i > 1)
- m_ITGraph->SetNumProperty("ConnectTo", i-1, rand() % (i-1));
- }
- m_ITGraph->SetNumProperty("Redraw", oldRedraw);
- }
- }
-
- void CItgDemoView::RandomDirected(int NodeCount)
- {
- if(AskToSave()) {
- int oldRedraw = (int)m_ITGraph->GetNumProperty("Redraw");
- m_ITGraph->SetNumProperty("Redraw", FALSE);
- m_ITGraph->SendMessage(VBM_METHOD, METH_CLEAR, 0L);
- for(int i = 1; i <= NodeCount; i++) {
- char szNodeName[16];
- sprintf(szNodeName, "Node #%d", i);
- m_ITGraph->AddItem(szNodeName, -1);
- if(m_bRandomSizedNodes) {
- m_ITGraph->SetNumProperty("ItemWidth", rand()%200 + 25, i-1);
- m_ITGraph->SetNumProperty("ItemHeight", rand()%200 + 25, i-1);
- }
- if(i > 1)
- for(int j = rand()%3; j >= 0; j--)
- m_ITGraph->SetNumProperty("ConnectTo", i-1, rand() % (i-1));
- }
- m_ITGraph->SetNumProperty("Redraw", oldRedraw);
- }
- }
-
- int CItgDemoView::AskToSave()
- {
- if(m_ITGraph->GetNumProperty("IsDirty")) {
- CDocument *pDoc = GetDocument();
- int wSave;
- if(pDoc->GetPathName() == "")
- wSave = MessageBox("Save changes made to the current graph?", "Save Graph?",
- MB_YESNOCANCEL | MB_ICONQUESTION);
- else
- wSave = MessageBox("Save changes made to '" + pDoc->GetTitle() + "'?", "Save Graph?",
- MB_YESNOCANCEL | MB_ICONQUESTION);
- if(wSave == IDCANCEL)
- return FALSE;
- else if(wSave == IDYES) {
- if(pDoc->GetPathName() != "")
- m_ITGraph->SetStrProperty("SaveAs", pDoc->GetPathName());
- else {
- CFileDialog dlgSaveAs(FALSE, "itg", "*.itg", OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR | OFN_HIDEREADONLY,
- "ITGraph Files (*.itg) | *.itg | All Files (*.*) | *.* ||", this);
- if(dlgSaveAs.DoModal() == IDOK) {
- pDoc->SetPathName(dlgSaveAs.GetPathName());
- m_ITGraph->SetStrProperty("SaveAs", pDoc->GetPathName());
- } else
- return FALSE;
- }
- }
- }
-
- return TRUE;
- }
- void CItgDemoView::OnLinedblclickItgraph1(UINT, int, CWnd*, LPVOID lpParams)
- {
- short Button = AFX_NUM_EVENTPARAMINDEX(short, lpParams, 3);
- short ToIx = AFX_NUM_EVENTPARAMINDEX(short, lpParams, 4);
- short FromIx = AFX_NUM_EVENTPARAMINDEX(short, lpParams, 5);
-
- if(Button == ITG_LeftButton) {
- m_ITGraph->SetNumProperty("ConnectTo", ToIx, FromIx);
- CDlgLine dlgLine(m_ITGraph, GetParent());
- dlgLine.DoModal();
- }
- }
-